package com.kele.system.controller;

import com.kele.base.common.exception.AppException;
import com.kele.base.model.response.ResponseResult;
import com.kele.system.model.entity.Menu;
import com.kele.system.service.UserService;
import com.kele.system.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.util.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 菜单控制器
 * @Author: Kele
 * @CreateDate: 2023/4/22 16:45
 * @Version:
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private UserService userService;

    @GetMapping("/getMenu")
    public ResponseResult getMenu(){
        SecurityUtil.User user = SecurityUtil.getUser();
//        System.out.println(user);
        if (user == null || user.getId() == null || StringUtil.isEmpty(user.getId())){
            AppException.cast("用户未登录");
        }

        List<Menu> menuList=userService.findMenuByUserId(user.getId());
        List<Map<String,Object>> menuMapList = new ArrayList<>();
        if(menuList!=null && menuList.size()>0){
            for(Menu menu:menuList){
                if(menu.getParentId().equals("1")){
                    Map<String, Object> menuMap=new HashMap<>();
                    menuMap.put("title",menu.getMenuName());
                    menuMap.put("component",menu.getUrl());
                    menuMap.put("name",getConvert(menu.getCode()));
                    Map<String, Object> map=new HashMap<>();
                    map.put("title",menu.getMenuName());
                    menuMap.put("meta",map);
                    menuMap.put("path",menu.getCode());
                    List<Map<String,Object>> sonMenuMapList=new ArrayList<>();
                    for(Menu sonMenu:menuList){
                        if(sonMenu.getParentId().equals(menu.getId())){
                            Map<String, Object> sonMenuMap=new HashMap<>();
                            sonMenuMap.put("title",sonMenu.getMenuName());
                            sonMenuMap.put("component",sonMenu.getUrl());
                            sonMenuMap.put("name",getConvert(sonMenu.getCode()));
                            Map<String, Object> map1=new HashMap<>();
                            map1.put("title",sonMenu.getMenuName());
                            sonMenuMap.put("meta",map1);
                            sonMenuMap.put("path",sonMenu.getCode());
                            sonMenuMap.put("children","");
                            sonMenuMapList.add(sonMenuMap);
                        }
                    }
                    menuMap.put("children",sonMenuMapList);
                    menuMapList.add(menuMap);
                }
            }
        } else {
            AppException.cast("用户菜单列表为空");
        }
        return ResponseResult.success(menuMapList);
    }

    public static String getConvert(String str) {
        String first = str.substring(0, 1);
        String after = str.substring(1); //substring(1),获取索引位置1后面所有剩余的字符串
        first = first.toUpperCase();
        after = after.toLowerCase();
        return  first + after;
    }
}
